import SnowflakeId from "snowflake-id";
const { Op } = require("sequelize");
const UserModels = require('../models/user');

interface searchInfo {
    id?: number,
    username?: string,
    password?: string,
}
interface user {
    id?: number,
    logname?: string,
    password?: string,
    headimage?: string,
    mobilephone?: string,
    name?: string,
    role?: number,
    sex?: string,
    status?: number,
    email?: string,
}
class UserService {
    public async createUser(params) {
        const id = new SnowflakeId();
        params.id = id.generate();
        const res: any = await UserModels.create(params)
        return res.dataValues;
    }
    public async getUserInfo({ id, username }: searchInfo) {
        const whereOpt = {}
        username && Object.assign(whereOpt, { logname: username })
        id && Object.assign(whereOpt, { id })
        const res: any = await UserModels.findOne({
            where: whereOpt
        })
        return res ? res.dataValues : null
    }
    public async updateById(id: number, newUser) {
        const whereOpt = { id }
        const res = await UserModels.update(newUser, { where: whereOpt })
        return res[0] > 0
    }
    // 获取普通用户列表
    public async getUserList() {
        const res: any = await UserModels.findAndCountAll({
            where: {
                role: {
                    [Op.lte]: 8
                }
            },
        })
        return res ? res : null
    }
    // 分页获取普通用户列表
    public async getUserListByPageInfo(pageNum: number, pageSize: number) {
        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await UserModels.findAndCountAll({
            where: {
                role: {
                    [Op.lte]: 8
                }
            },
            offset: offset,
            limit: pageSize * 1
        })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
    // 根据id删除用户
    public async deleteById(id: number) {
        const res: any = await UserModels.destroy({
            where: { id }
        })
        return res > 0
    }
    // 根据用户名模糊搜索用户
    public async searchUsers({ name, pageNum, pageSize }) {
        if (pageNum && pageSize) {
            // 分页获取
            const offset = (pageNum - 1) * pageSize
            const { count, rows } = await UserModels.findAndCountAll({
                where: {
                    name: {
                        [Op.like]: `%${name}%`,
                    }
                },
                offset: offset,
                limit: pageSize * 1
            })
            return {
                pageNum,
                pageSize,
                total: count,
                list: rows
            }
        } else {
            // 不分页直接获取
            const { count, rows } = await UserModels.findAndCountAll({
                where: {
                    name: {
                        [Op.like]: `%${name}%`,
                    }
                }
            })
            return {
                total: count,
                list: rows
            }
        }
    }
}

module.exports = new UserService()